import $ from '../common';
Page({
  data: {
    icon:'https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/f91e5fe4-cf2e-11e7-9713-44032ca554a5.png',
    sexlist:['男','女'],
    sexparamslist:['male','female'],
    sexidx:0,
    avatar:'',
    gender:''
  },
  onLoad(e) {
    this.getinfo()
  },
  onShow() { },
  addavatar(){//头像
    wx.chooseImage({
      count:1,
      sizeType: "compressed",
      success: (res) => {
        var newList = res.tempFilePaths
        $.uploadFileList('/image/upload/oss', {
            fileList: newList,
            prefix: "keday/"
          },
          (res) => {
            this.setData({
              avatar: res
            })
            this.updata()
          },
          (err) => {
            console.log('上传失败:', err)
          }
        )
      }
    })
  },
  updata(){//更新
    $.post('/user/info/update',{
      avatar:this.data.avatar||'',
      gender:this.data.gender||'',
    },res=>{
      this.getinfo()
    })
  },
  getinfo(){//获取数据
    $.get('/user/info',{},res=>{
      let a =this.data.sexparamslist.indexOf(res.user.gender?res.user.gender:'male')
      this.setData({
        userinfo:res.user,
        sexidx:a,
        avatar:'',
        gender:''
      })
    })
  },
  bindMultiPickerChange(e){//性别
    this.setData({
      gender:this.data.sexparamslist[e.detail.value]
    })
    this.updata()
  },
  handle(e){
    const {value=''}=e.currentTarget.dataset
    let obj={
      key:'nickname',
      tit:'设置真实姓名',
      value:value,
      url:'/user/info/update',
      inputtype:'text'
    }
    $.navigateTo(`/address/childupdata?info=${JSON.stringify(obj)}`)
  },
  goamend(){//跳转到注册
    $.navigateTo(`/login/amend?type=Modify&phone=${this.data.userinfo.phone}`)
  },
})